package com.oceansoft.core.jxkh.dao.impl;

import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.jxkh.dao.IKhmcDao;
import com.oceansoft.core.jxkh.domain.Khmc;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * Created by OS on 2017/12/14.
 */

@Repository
public class KhmcDaoImpl implements IKhmcDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Pagination findAllkhmc(HashMap<String, String> map) {
        int offset = Integer.parseInt(map.get("offset").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        //String sql = "select c.dbid,c.itembId,c.name,c.score,c.status,b.name khlxName from t_pskh_check_itemc c inner join t_pskh_check_itemb b on c.itembId = b.dbid order by c.dbid ";
        String sql = "select c.*,b.name khlxName,a.name khyName from (t_pskh_check_itemc c inner join t_pskh_check_itemb b on c.itembId = b.dbid) inner join t_pskh_check_itema a on b.itemaId = a.dbid order by c.dbid";
        String count_sql = PageUtil.getCountSQL(sql.toString());
        System.out.println(sql.toString());
        int _total = jdbcTemplate.queryForInt(count_sql);
        String page_sql = PageUtil.getPageSQL(sql.toString(), offset,limit);
        List list = jdbcTemplate.queryForList(page_sql);
        return new Pagination(list,_total,limit, (offset/limit)+1);
    }

    @Override
    public void editKhmcInfo(Khmc khmc) {
        String sql = "update t_pskh_check_itemc set itembId = ? , name = ? , score = ? , status = ? where dbid = ?";
        jdbcTemplate.update(sql,khmc.getItembId(),khmc.getName(),khmc.getScore(), khmc.getStatus() , khmc.getDbid());
    }

    @Override
    public Khmc findKhmcInfoByDbid(int dbid) {
        String sql = "select c.dbid,c.itembId,c.name,c.score,c.status,b.name khlxName,a.name khyName from (t_pskh_check_itemc c inner join t_pskh_check_itemb b on c.itembId = b.dbid) inner join t_pskh_check_itema a on b.itemaid = a.dbid where c.dbid = ?";
        Khmc khmc = jdbcTemplate.queryForObject(sql, new Object[]{dbid}, new RowMapper<Khmc>() {
            @Override
            public Khmc mapRow(ResultSet rs, int args) throws SQLException {
                Khmc khmc = new Khmc();
                khmc.setDbid(rs.getInt("dbid"));
                khmc.setItembId(rs.getInt("itembId"));
                khmc.setName(rs.getString("name"));
                khmc.setScore(rs.getInt("score"));
                khmc.setStatus(rs.getInt("status"));
                khmc.setKhlxName(rs.getString("khlxName"));
                khmc.setKhyName(rs.getString("khyName"));
                return khmc;
            }
        });
        return khmc;
    }

    @Override
    public void deleteKhmcInfoByDbid(int dbid) {
        String sql = "delete from t_pskh_check_itemc where dbid = ?";
        jdbcTemplate.update(sql, new Object[]{dbid});
    }

    @Override
    public void addKhmcInfo(Khmc khmc) {
        String sql = "insert into t_pskh_check_itemc(dbid,itembId,name,score,status) values(SEQ_PSKH_CHECK_ITEMC.NEXTVAL,?,?,?,?)";
        jdbcTemplate.update(sql,khmc.getItembId(),khmc.getName(),khmc.getScore(),khmc.getStatus());
    }

    @Override
    public void batchDeleteKhmcInfo(final int[] dbids) {
        String sql = "delete from t_pskh_check_itemc where dbid = ?";
        int[] delCounts = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setInt(1,dbids[i]);
            }
            @Override
            public int getBatchSize() {
                return dbids.length;
            }
        });
    }
}
